package com.sk.skplanet.commute.views;

import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sk.skplanet.commute.R;
import com.sk.skplanet.commute.actions.APIRequestBus2;
import com.sk.skplanet.commute.actions.APIRequestBus2.OnBusRequestCompleted;
import com.sk.skplanet.commute.actions.APIRequestBus2.RequestMethod;
import com.sk.skplanet.commute.controllers.DialogController;
import com.sk.skplanet.commute.vo.API_RequestInfo;
import com.sk.skplanet.commute.vo.GetStationByRouteVo;

public class AddStopDetailStopList extends ListActivity implements OnBusRequestCompleted {
	private Bundle bundle;
	private Intent intentToReturn;
	private MyArrayAdapter adapter;
	private List<GetStationByRouteVo> itemList;
	private ActionBar actionBar;
	private TextView txtNoItems;
	private ProgressBar progressBar;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_stop_detail_stop_list);
		
		txtNoItems = (TextView)findViewById(R.id.txt_stop_list_no_items);
		progressBar = (ProgressBar)findViewById(R.id.progressBar);
		
		bundle = new Bundle();
		intentToReturn = new Intent();
		
		APIRequestBus2 request = new APIRequestBus2(0, getIntent().getStringExtra("busCode"), this);
		request.execute(RequestMethod.API_GETSTATIONBYROUTELIST);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_stop_detail_stop_list, menu);
		actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle(R.string.please_wait);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case android.R.id.home:
	            finish();
				break;
			default:
				return false;
		}
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		bundle.putString("stopName", itemList.get(position).getStationNm());
		bundle.putString("stopCode", itemList.get(position).getStationNo());
		bundle.putString("busType", itemList.get(position).getRouteType());
		intentToReturn.putExtras(bundle);
		this.setResult(RESULT_OK, intentToReturn);
		this.finish();
	}
	
	/**
	 * 델리게이트 - 버스노선 정보 Retrieve 완료
	 */
	@Override
	public void onBusRequestCompleted(int requestCode, List<?> infoData) {
		itemList = (List<GetStationByRouteVo>)infoData;
		if(itemList != null && itemList.size() > 0) {
			actionBar.setTitle(itemList.get(0).getBusRouteNm());
			adapter = new MyArrayAdapter(this);
			setListAdapter(adapter);
			adapter.notifyDataSetChanged();
		} else {
			actionBar.setTitle(R.string.list_stop);
			txtNoItems.setText(R.string.no_result);
		}
		
		progressBar.setVisibility(View.INVISIBLE);
	}

	private class MyArrayAdapter extends ArrayAdapter {
		Context context;
		
		MyArrayAdapter(Context context) {
			super(context, R.layout.listview_search_bus_info, itemList);
			this.context = context;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			GetStationByRouteVo item = itemList.get(position);
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			View row = (View)inflater.inflate(R.layout.listview_search_bus_info, null);
			
			TextView stopName = (TextView)row.findViewById(R.id.listview_search_bus_text_busno);
			TextView nextStop = (TextView)row.findViewById(R.id.listview_search_bus_text_bustype);
				
			row.setTag("stopcode");
//			busName.setText(busNameList.get(position));
			
			stopName.setText(item.getStationNm());
			if(position + 1 < itemList.size())
				nextStop.setText("다음 : " + itemList.get(position + 1).getStationNm() + " (" + itemList.get(position).getStationNo() + ")");
			else
				nextStop.setText("마지막 정류장입니다.");
			
			return row;
		}
		
		
	}
}
